
local Input = import("./Input.lua");
local Statement = commonlib.inherit(Input);

function Statement:ctor()
end

function Statement:Init(block, opt)
    Statement._super.Init(self, block, opt);
    self.__input_connection__:SetType("next_connection");
    return self;
end

function Statement:Render()
    local leftUnitCount, topUnitCount = self:GetLeftTopUnitCount();
    local widthUnitCount, heightUnitCount = self:GetWidthHeightUnitCount();
    local blockWidthUnitCount = self:GetBlock():GetWidthHeightUnitCount();
    self:SetBrushColor(self:GetBlock():GetColor());
    self:DrawInputStatement(blockWidthUnitCount, heightUnitCount, leftUnitCount, topUnitCount, true);
    local inputBlock = self:GetInputBlock();
    if (not inputBlock) then return end
    inputBlock:Render()
end

function Statement:UpdateWidthHeightUnitCount()
    local inputBlock = self:GetInputBlock();
    local inputBlockWidthUnitCount, inputBlockHeightUnitCount = 0, 8;
    if (inputBlock) then _, _, _, _, inputBlockWidthUnitCount, inputBlockHeightUnitCount = inputBlock:UpdateWidthHeightUnitCount() end
    return self.STATEMENT_WIDTH_UNIT_COUNT + inputBlockWidthUnitCount, self.CONNECTION_HEIGHT_UNIT_COUNT * 2 + inputBlockHeightUnitCount;
end

function Statement:UpdateLeftTopUnitCount()
    local inputBlock = self:GetInputBlock();
    if (not inputBlock) then return end
    local leftUnitCount, topUnitCount = self:GetLeftTopUnitCount();
    inputBlock:SetLeftTopUnitCount(leftUnitCount + self.STATEMENT_WIDTH_UNIT_COUNT, topUnitCount + self.CONNECTION_HEIGHT_UNIT_COUNT);
    inputBlock:UpdateLeftTopUnitCount();
end

function Statement:OnSizeChange()
    local leftUnitCount, topUnitCount = self:GetLeftTopUnitCount();
    local blockWidthUnitCount, blockHeightUnitCount = self:GetBlock():GetWidthHeightUnitCount();
    self.__input_connection__:SetLeftTopUnitCount(leftUnitCount + self.STATEMENT_WIDTH_UNIT_COUNT, topUnitCount);
end

function Statement:ConnectionBlock(block)
    if (block.__previous_connection__ and not block.__previous_connection__:IsConnection() and self.__input_connection__:IsMatch(block.__previous_connection__)) then
        self:GetBlockly():RemoveBlock(block);
        local inputConnectionConnection = self.__input_connection__:Disconnection();
        self.__input_connection__:Connection(block.__previous_connection__);
        local blockLastNextBlock = block:GetLastNextBlock();
        if (blockLastNextBlock.__next_connection__) then 
            blockLastNextBlock.__next_connection__:Connection(inputConnectionConnection); 
        else 
            self:GetBlockly():AddBlock(nextConnectionConnection:GetBlock(), true);
        end
        self:GetTopBlock():UpdateLayout();
        return true;
    end

    local inputBlock = self:GetInputBlock();
    return inputBlock and inputBlock:ConnectionBlock(block);
end

function Statement:GetMouseUI(x, y)
    local left, top = self:GetLeftTop();
    local width, height = self:GetWidthHeight();
    local block_width = self:GetBlock():GetWidthHeight();
    local UnitSize = self:GetUnitSize();
    local connection_height = self.CONNECTION_HEIGHT_UNIT_COUNT * UnitSize;
    local statement_width = self.STATEMENT_WIDTH_UNIT_COUNT * UnitSize;
    -- 区域内判断
    width = math.max(width, block_width);
    if (x < left or x > (left + width) or y < top or y > (top + height)) then return nil end
    
    -- 左则矩形, 上下连接 则返回当前UI
    if (x <= (left + statement_width) or y <= (top + connection_height) or y >= (top + height - connection_height)) then return self end 
    
    local inputBlock = self:GetInputBlock();
    return inputBlock and inputBlock:GetMouseUI(x, y);
end

return Statement;